<?
require_once('./application/classes/item/activiteiten.class.php');

class activiteitenModel extends db implements db_interface{

private static $instance;

public function __construct($iDb){
	
		parent::__construct($iDb);
	
	}
	
public static function getInstance($iDb){
	
		if(self::$instance == NULL){
			self::$instance = new self($iDb);
		}
		
		return self::$instance;
	
	
	}
	
		
	
	
	public function getAllActiviteiten(user_bewonersgroep $iUser){
	
		$query = $this -> db -> prepare('select i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as "date",i.item_geb_id,DATE_FORMAT(a.activi_datum,"%d/%m/%Y") as aDate,a.activi_beschrijving,b.bew_titel from item i join activiteiten a on (i.item_id = a.activi_id) join bewonersgroep b on(i.item_geb_id = b.bew_id) where i.item_geb_id = ? and i.item_deleted = 1 order by i.item_id desc;');
		
		$bindValue = $iUser -> id;
		
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$title,$date,$geb_id,$aDate,$text,$naam); 
		
		$returnArray = array();
		
		while ($query -> fetch())
		{
			$activi = new item_activiteiten($this -> db);
			$activi -> id = $id;
			$activi -> title = $title;
			$activi -> geb_naam = $naam;
			$activi -> act_beschrijving = $text;
			$activi -> geb_id = $geb_id;
			$activi -> datum = $date;
			$activi -> act_date = $aDate;
			$returnArray[] = $activi;
		}
		
		return $returnArray;
	
	
	}
	
	public function addActiviteit(item_activiteiten $activi){
		$query = $this -> db -> prepare("CALL activiAdd(?,?,?,?,?)");	
		$bindingvars=array($activi -> act_beschrijving , $activi -> title , $activi -> geb_id,$activi -> act_date , 'activiteit');
		$this -> bindVars($query,$bindingvars);
		$query -> execute();
	}
	public function delBericht(item_bericht $bericht)
	{
		
		$query = $this -> db -> prepare('call itemDel(?);');
		$bindValue = $bericht -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
	}
	public function updactiviteit(item_activiteiten $activi)
	{
		$query = $this -> db -> prepare('call activiUpd(?,?,?,?);');
		$bindingVars = array($activi -> title , $activi -> act_beschrijving,$activi -> act_datum , $activi -> id);
		$this -> bindVars($query,$bindingVars);
		$query -> execute();
	}
	
	
	public function fill()
	{
		$query = $this -> db -> query('SELECT i.item_id,bg.bew_titel,g.geb_id,i.item_title,b.bericht_text,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as date FROM item i join gebruikers g on (i.item_geb_id = g.geb_id) join bewonersgroep bg on (bg.bew_id = g.geb_id) join bericht b on (i.item_id = b.bericht_id) where i.item_deleted = 1 ORDER BY i.item_id DESC LIMIT 5');
		
		
		
		$berichten = array();
		while ($row = $query->fetch_assoc())
		{
			$bericht = new item_bericht($this -> db);
			$bericht -> id = $row['item_id'];
			$bericht -> title = $row['item_title'];
			$bericht -> geb_naam = $row['bew_titel'];
			$bericht -> bericht_text = $row['bericht_text'];
			$bericht -> geb_id = $row['geb_id'];
			$bericht -> datum = $row['date'];
			$berichten[]= $bericht;
		}
		return $berichten;
	}
	
	
	public function getActiviId(item_activiteiten $activi)
	{
		$query = $this -> db -> prepare('select i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as "date",i.item_geb_id,DATE_FORMAT(a.activi_datum,"%d/%m/%Y") as aDate,a.activi_beschrijving,b.bew_titel from item i join activiteiten a on (i.item_id = a.activi_id) join bewonersgroep b on(i.item_geb_id = b.bew_id) where i.item_id = ? and i.item_deleted = 1;');
		
		$bindValue = $activi -> id;
		$query -> bind_param('i', $bindValue);
		$query -> execute();
		
		$query -> bind_result($id,$title,$date,$geb_id,$aDate,$text,$naam); 
		
		$returnArray = array();
		
		$query -> fetch();
			

			$activi -> title = $title;
			$activi -> geb_naam = $naam;
			$activi -> act_beschrijving = $text;
			$activi -> geb_id = $geb_id;
			$activi -> datum = $date;
			$activi -> act_date = $aDate;

			
		
		
		
		
	}
	
		public function getActiviByDate($date)
	{
		$query = $this -> db -> prepare('select i.item_id,i.item_title,DATE_FORMAT(i.item_datum, "%H:%i - %d/%m/%y") as "date",i.item_geb_id,DATE_FORMAT(a.activi_datum,"%d/%m/%Y") as aDate,a.activi_beschrijving,b.bew_titel from item i join activiteiten a on (i.item_id = a.activi_id) join bewonersgroep b on(i.item_geb_id = b.bew_id) where DATE_FORMAT(a.activi_datum,"%d/%c/%Y") = ? and i.item_deleted = 1;');
	
		$bindValue = $date;
		$query -> bind_param('s', $bindValue);
		
		
		$query -> execute();
		
		
		
		$query -> bind_result($id,$title,$date,$geb_id,$aDate,$text,$naam); 
		
	
		
		$res = array();
		
		while($query -> fetch()){
			
			
			$activi = new item_activiteiten($this -> db);
			$activi -> id = $id;
			$activi -> title = $title;
			$activi -> geb_naam = $naam;
			$activi -> act_beschrijving = $text;
			$activi -> geb_id = $geb_id;
			$activi -> datum = $date;
			$activi -> act_date = $aDate;
			$res[] = $activi;
			
		}
	
		return $res;
		
		
	}

	
	
}



?>